var closeWindow = function() {
    $('#include').hide();    
    $("#pages").show();
    $("#general").hide();
}
var navToGeneral = function() {
    $("#pages").hide();
    $("#general").show();
    $("div.sub_nav img").each(function() {
        var imgSrc = $(this).attr("src");        
        if (imgSrc.indexOf("_off") != -1) {
            $(this).attr("src", imgSrc.replace("_off", "_on"));
        } else if (imgSrc.indexOf("_on") != -1) {
            $(this).attr("src", imgSrc.replace("_on", "_off"));
        }
    });
    return false;
}
var navToPage = function() {
    $("#pages").show();
    $("#general").hide();
    $("div.sub_nav img").each(function() {
        var imgSrc = $(this).attr("src");        
        if (imgSrc.indexOf("_off") != -1) {
            $(this).attr("src", imgSrc.replace("_off", "_on"));
        } else if (imgSrc.indexOf("_on") != -1) {
            $(this).attr("src", imgSrc.replace("_on", "_off"));
        }
    });
    return false;
}


var qId = null;
var lightColorFlag = "_light.";
var itemId = "";
var qStatus = null;
var qRefid = "";
var qVersion = "";
var qAvailability = "";
var qText = "";
var qSetId = "";
var itemParams = "";
var qSequence = "";
var qName = "";
var openflag = false;
var search = function() {   
	var THIS = this; 
    var keyword = $("#keywords").val();    
    $.post('searchpage.html',{        
        keyword: keyword
    },
    function(data) {
        //success
        $("#qresults").html(data);
        if(data.length > 0){
            $("#dialogmessage").text(data.split("</div>").length-1+" rows found matching your criteria");
            THIS.checkHaveStyle();
        }
        else{            
            $("#dialogmessage").text("No Pages matched the entered criteria");
        }    
    });
}
var checkHaveStyle = function(){
	var includeObjs = $("div.question").children();
	var includeObjString = ",";
	includeObjs.each(function(){
		var thisId = $(this).attr("id");
		thisId = thisId.replace("questionSet","");
		includeObjString += thisId+",";		
	});
	var includeList=includeObjString.split(",");	
	var allObjs = $("#qresults").children();
	allObjs.each(function(){
		var ThisID = $(this).attr("id");		
		if (includeList.indexOf(ThisID)>0){
			$(this).addClass("visited");
		}		
	});
}
var includeSet = function(naireId){
    $("#Msg_HaveIt").hide();
    $("#Msg_IncludeIt").hide();
    $("#Msg_Retire").hide();
    $("#Msg_SelectOne").hide(); 
    var THIS = this;       
    if (qId != null) {
        if (qStatus != 'retired') {
            if (checkHave(qId)){
                //$("div.message h3").html("It already included.");                
                $("#Msg_SelectOne").hide();
                $("#Msg_IncludeIt").hide();
                $("#Msg_Retire").hide();
                $("#Msg_HaveIt").show();
                return false;
            }else{            	
                var insertHtml = addHtml();                
                $("div.question").append(insertHtml);
                THIS.checkHaveStyle();
                $("#Msg_SelectOne").hide();
                $("#Msg_HaveIt").hide();   
                $("#Msg_Retire").hide();
                $("#Msg_IncludeIt").show();                                
                var maxInclude = $("div.question").html().toLowerCase().split("<table").length;
                $("#pagemessage").html("This Page contains the following "+(Number(maxInclude)-1) + " Pages:");
            //$("div.error h3").html("This Dialog contains the following " + ($("#question table").size() + 1) + " Pages:");
            }
        }else{
            // $("div.message h3").html("<font color='red'>Retired questionSet cannot be included</font>");            
            $("#Msg_SelectOne").hide();
            $("#Msg_HaveIt").hide();
            $("#Msg_IncludeIt").hide();
            $("#Msg_Retire").show();
        }

    }else{
        alert("Please select a Page before choosing Include Page");
    }
}
var addHtml = function(){    
    var theSequence = $("div.question").html().toLowerCase().split("<table").length;    
    var str1 = "<table onclick=\"itemClick(this,"+qSetId+",'"+qAvailability+"','"+qStatus+"')\" width='100%' border='0' cellpadding='0' cellspacing='0' id='questionSet"+qSetId+"'";
    var str2 = "title='"+qText+"' qsId='"+qSetId+"' sequence='"+theSequence+"'>";
    var str3 = "        <tbody>";
    var str4 = "            <tr>";
    var str5 = "                <th width='5%' class='number0' id ='sequence'>"+theSequence+"</th>"+"                <td width='15%' class='row1'>"+qName+"</td>";
    var str6 = "                <td width='18%' class='row2'>"+qRefid+"</td>";
    var str7 = "                <td width='8%' class='row2'>Ver "+qVersion+"</td>";
    var str8 = "                <td width='16%' class='row3'>";
    var str9 = qAvailability;
    var str10 = "                    /";
    var str11 = qStatus;
    var str12 = "                </td>";
    var str13 = "                <td width='46%' class='row4'>";
    var str14 = qText;
    var str15 = "                </td>";
    var str16 = "            </tr>";
    var str17 = "        </tbody>";
    var str18 = "    </table>";
    return str1+str2+str3+str4+str5+str6+str7+str8+str9+str10+str11+str12+str13+str14+str15+str16+str17+str18;
}
var checkHave = function(qId){
    var includeObjs = $("div.question").children();
	var includeObjString = ",";
	includeObjs.each(function(){
		var thisId = $(this).attr("id");
		thisId = thisId.replace("questionSet","");		
		includeObjString += thisId+",";		
	});	
	var includeList=includeObjString.split(",");	
	var checkFlag = false; 
	for (i=0;i<includeList.length;i++){
		if (includeList[i]==qId){
			checkFlag = true;
		}
	} 
	return checkFlag; 
}
function qitemHover(item) {
    $("div.data_tbody div").removeClass("hover");
    $(item).addClass("hover");
}

function qitemClick(item, id,status,setId) {
    $("#Msg_HaveIt").hide();
    $("#Msg_IncludeIt").hide();
    $("#Msg_Retire").hide();
    $("#Msg_SelectOne").hide();
    $("div.data_tbody div").removeClass("active");
    var visitedCss = $(item).attr("class");
    if (visitedCss.indexOf("visited")>0){
    	$(item).removeClass("visited"); 
    }       
    $(item).addClass("active");
    var lastChild = $(item).children().children().children();    
    qRefid = $(lastChild).children("#refId").text();
    qVersion = $(lastChild).children("#version").text();
    qAvailability = $(lastChild).children("#availability").text();
    qText = $(lastChild).children("#qtext").text();
    qName = $(lastChild).children("#name").text();
    qId = id;
    qStatus = status;
    qSetId = setId;
    if (visitedCss.indexOf("visited")>0){
    	$(item).addClass("visited"); 
    }
}

function qitemMouseOut(item) {
	//$(item).removeClass("visited");
    $(item).removeClass("hover");
}
function itemClick(item,itemValue,availability,status) {
    $("#Msg_Retire").hide();
    $("#Msg_Locked").hide();
    qStatus = status;
    openflag = false;
    qAvailability = availability;
    var btns = document.getElementsByName("qbtn");    
    if($(item).attr("class").indexOf("active") == -1){
        $("div.question table").removeClass("active");
        $(item).addClass("active");        
        itemParams = itemValue;
        qSequence = $(item).attr("sequence");        
        for (i = 0; i < btns.length; i++) {
            var btn = btns[i];
            var imgSrc = $(btn).attr("src");
            var newSrc = imgSrc.replace(lightColorFlag, '.');
            $(btn).attr("src", newSrc);
        }
        if (availability == 'locked' || status == 'retired'){
            openflag = true;
            var imgSrc = $("#editButton").attr("src");            
            if (imgSrc.indexOf("_light")==-1){
                var newSrc = imgSrc.replace('.',lightColorFlag);
                $("#editButton").attr("src", newSrc);
            }else{
                 var newSrc = "btn/functions2/btn_edit_light.gif";
                $("#editButton").attr("src", newSrc);
            }
            
        }
    }
    else {
        $("div.question table").removeClass("active");
        itemParams = "";
        qSequence = "";
        for (i = 0; i < btns.length; i++) {
            var btn = btns[i];
            $(btn).removeClass("btn");
            var imgSrc = $(btn).attr("src");
            if (imgSrc.indexOf("_light")==-1){
                var newSrc = imgSrc.replace(".", lightColorFlag);
            	$(btn).attr("src", newSrc);
            }
            
        }
    }
}

var remove = function(){
    if (itemParams != "") {        
            var item = $("[sequence=" + qSequence + "]");
            item.remove();
            var maxInclude = $("div.question").html().toLowerCase().split("<table").length;           
            var lastSequence = Number(qSequence)+1;             
            while (lastSequence <= maxInclude){                               
                var thisItem = $("[sequence=" + lastSequence + "]");                
                $(thisItem).each(function() {
                    $("th", this).html(lastSequence-1);
                    $("TH", this).html(lastSequence-1);
                    $(this).attr("sequence", lastSequence-1);
                });
                lastSequence++;
            }
            itemParams = "";
            $("#pagemessage").html("This Page contains the following "+(Number(maxInclude)-1) + " Pages:");
            return false;       
    }else{
        alert("Please select an Item !");
    }
}
var up = function(){    
    if (itemParams != "") {
        if (qSequence <= 1){
            return false;
        }
        var lastItem = $("[sequence=" + qSequence + "]");        
        var upSequence = Number(qSequence-1);
        var upItem = $("[sequence=" + upSequence + "]");        
        $(lastItem).each(function() {            
            $("th", this).html(upSequence);
            $("TH", this).html(upSequence);            
            $(this).attr("sequence", upSequence);
        });
        $(upItem).each(function() {            
            $("th", this).html(qSequence);
            $("TH", this).html(qSequence);
            $(this).attr("sequence", qSequence);
        });
        upItem.insertAfter(lastItem);
        qSequence = upSequence;
        return false;
    } else {
        alert("Please select an Item !");
    }

}
var down = function(){
    if (itemParams != "") {
        var lastItem = $("[sequence=" + qSequence + "]");
        var upSequence = Number(qSequence)+1;
        var upItem = $("[sequence=" + upSequence + "]");
        var maxInclude = $("div.question").html().toLowerCase().split("<table").length-1;
        
        if (upSequence > maxInclude){
            return false;
        }

        $(lastItem).each(function() {            
            $("th", this).html(upSequence);
            $("TH", this).html(upSequence);
            $(this).attr("sequence", upSequence);
        });
        $(upItem).each(function() {            
            $("th", this).html(qSequence);
            $("TH", this).html(qSequence);
            $(this).attr("sequence", qSequence);
        });
        lastItem.insertAfter(upItem);
        qSequence = upSequence;
        return false;
    } else {
        alert("Please select an Item !");
    }
}
function alinkAction(action, isBlank) {    
    if (itemParams == "") {
        alert("Please select an item.");
    } else if (!isBlank) {       
        if (qStatus == 'retired'){
            $("#Msg_Retire").show();
            return false;
        }
        if (qAvailability == 'locked'){
            $("#Msg_Locked").show();
            return false;
        }
        if (!openflag){
            window.location.href = action + ".html?id=" + itemParams;
        }
        
    } else {
        if (qStatus == 'retired'){
            $("#Msg_Retire").show();
            return false;
        }
        if (qAvailability == 'locked'){
            $("#Msg_Locked").show();
            return false;
        }
        if (!openflag){
            window.open(action + ".html?id=" + itemParams);
        }
    }
}
function blinkAction(action, isBlank) {
    if (itemParams == "") {
        alert("Please select an item.");
    } else if (!isBlank) {        
        if (!openflag){
            window.location.href = action + ".html?" + itemParams;
        }
        if (qStatus == 'retired'){
            $("#Msg_Retire").show();
            return false;
        }
        if (qAvailability == 'locked'){
            $("#Msg_Locked").show();
            return false;
        }

    } else {
        if (qStatus == 'retired'){
            $("#Msg_Retire").show();
            return false;
        }
        if (qAvailability == 'locked'){
            $("#Msg_Locked").show();
            return false;
        }
        if (!openflag){
            window.open(action + ".html?" + itemParams);
        }
    }
}
function linkAction(action, isBlank) {
    if (itemParams == "") {
        alert("Please select an item.");
    } else if (!isBlank) {
        window.location.href = action + ".html?id=" + itemParams;
    } else {
        window.open(action + ".html?id=" + itemParams);
    }
}
function editAction(submitAction) {
    var maxInclude = $("div.question").html().toLowerCase().split("<table").length;    
    var fromSequence = 1;
    var questionSet = "";
    while (fromSequence < maxInclude){
        var thisItem = $("[sequence=" + fromSequence + "]");
        questionSet += $(thisItem).attr("id")+";";        
        fromSequence++;
    }
    $("#includePage").val(questionSet);    
    afterSubmitRedirect("dialog", "edit.html", submitAction, "submitAction");
}
